package com.aditya.personalexpense;

import java.util.Vector;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.aditya.personalexpense.adapters.CategoryAdapter;
import com.aditya.personalexpense.controls.ColorPickerDialog;
import com.aditya.personalexpense.controls.ColorPickerDialog.OnColorSelectedListener;
import com.aditya.personalexpense.dataaccess.CategoryDA;
import com.aditya.personalexpense.database.DatabaseHelper;
import com.aditya.personalexpense.dataobject.CategoryDO;

public class CategoriesActivity extends Activity
{
	CategoryAdapter categoryAdapter;
	
	ListView lvCategories;
	Vector<CategoryDO> vecCategoryDO;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.categories_activity);
		
		new DatabaseHelper(CategoriesActivity.this);
		
		lvCategories = (ListView) findViewById(R.id.lvCategories);
		vecCategoryDO = new CategoryDA().getAllCategory();
		categoryAdapter = new CategoryAdapter(CategoriesActivity.this, vecCategoryDO);
		lvCategories.setAdapter(categoryAdapter);
		new DatabaseHelper(CategoriesActivity.this);
		
	}
	
	 @Override
	 public boolean onCreateOptionsMenu(Menu menu) 
	 {
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.category, menu);
	        return true;
	 }

	    @Override
	    public boolean onOptionsItemSelected(MenuItem item) 
	    {
	        switch (item.getItemId()) 
	        {
	            case R.id.add:
	            		showPopup();
	                return true;
	            
	            default:
	                return super.onOptionsItemSelected(item);
	        }
	    }

		private void showPopup() 
		{
			final AlertDialog.Builder alertDialog;
			LinearLayout view;
			Button btnOk;
			Button btnCancel;
			final AlertDialog dialog;
			final TextView tvtitle;
		    		alertDialog = new AlertDialog.Builder(CategoriesActivity.this);
					view = (LinearLayout) getLayoutInflater().inflate(R.layout.add_category_dialog, null); 
					alertDialog.setView(view);
					dialog = alertDialog.create();
					tvtitle = (TextView) view.findViewById(R.id.tvTitle);
					tvtitle.setText("Modify Category");
					final EditText edtCategoryName = (EditText) view.findViewById(R.id.edtCategoryName);
					final TextView tvColorCode = (TextView) view.findViewById(R.id.tvColorCode);
					
					final CategoryDO categoryDO = new CategoryDO();
					tvColorCode.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) 
						{
							
							  ColorPickerDialog colorPickerDialog = new ColorPickerDialog(CategoriesActivity.this, 123412, new OnColorSelectedListener() {
									
									@Override
									public void onColorSelected(int color) {
										 tvColorCode.setBackgroundColor(color);
							             categoryDO.colorcode = color;
									}
								});
						        colorPickerDialog.show();
						}
					});
					
					
					btnOk = (Button) view.findViewById(R.id.btnOk);
					btnCancel = (Button) view.findViewById(R.id.btnCancel);
					btnOk.setOnClickListener(new OnClickListener() 
					{
						@Override
						public void onClick(View v) 
						{
							if(edtCategoryName.getText().toString().equalsIgnoreCase(""))
							{
								Toast.makeText(CategoriesActivity.this, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
							}
							else
							{
								final String categoryName = edtCategoryName.getText().toString();
							
								new Thread(new Runnable() {
									
									@Override
									public void run() 
									{
										categoryDO.name = categoryName;
										
										new CategoryDA().insertCategory(categoryDO);
										
										new Handler(getMainLooper()).postDelayed(new Runnable() 
										{
											
											@Override
											public void run() {
												dialog.dismiss();
												
												categoryAdapter.refreshList(new CategoryDA().getAllCategory());
											}
										}, 500);
									}
								}).start();
							}
						}
					});
					btnCancel.setOnClickListener(new OnClickListener() 
					{
						
						@Override
						public void onClick(View v) 
						{
							dialog.dismiss();
						}
					});
					dialog.show();
			
		}

}
